package com.atguigu.gulimall.product.controller;

import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.R;
import com.atguigu.gulimall.product.entity.AttrEntity;
import com.atguigu.gulimall.product.entity.AttrGroupEntity;
import com.atguigu.gulimall.product.service.AttrAttrgroupRelationService;
import com.atguigu.gulimall.product.service.AttrGroupService;
import com.atguigu.gulimall.product.service.CategoryService;
import com.atguigu.gulimall.product.vo.AttrAttrGroupVo;
import com.atguigu.gulimall.product.vo.AttrGroupWithAttrsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * 属性分组
 *
 * @author syf
 * @email syf@gmail.com
 * @date 2024-11-27 14:46:11
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private CategoryService categoryService;
    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;

    //获取分类底下的所有属性分组和属性分组对应的属性
    @RequestMapping("/{catelogId}/withattr")
    public R attrGroupWithAttr(@PathVariable("catelogId") Long catelogId){
        List<AttrGroupWithAttrsVo> attrGroupWithAttrsVo = attrGroupService.getAttrGroupWithAttrByCatelogId(catelogId);
        List<AttrGroupWithAttrsVo> collect = attrGroupWithAttrsVo.stream().filter(attrGroupWithAttrsVo1 -> !attrGroupWithAttrsVo1.getAttrs().isEmpty()).collect(Collectors.toList());
        return R.ok().put("data",collect);
    }

    //保存属性组和属性的关系
    @RequestMapping("/attr/relation")
    public R addRelation(@RequestBody List<AttrAttrGroupVo> vos){

        attrAttrgroupRelationService.saveBatchA(vos);
        return R.ok();
    }

    //移除属性组里面的属性
    @RequestMapping("/attr/relation/delete")
    public R deleteRelation(@RequestBody AttrAttrGroupVo[] vos){
        //把vos变成list
        List<AttrAttrGroupVo> vos1 = Arrays.asList(vos);
        attrAttrgroupRelationService.removeAttrAttrGroupRelation(vos1);
        return R.ok();
    }

    //获取属性分组没有关联的属性
    @RequestMapping("/{attrgroupId}/noattr/relation")
    public R attrList(@PathVariable("attrgroupId") Long attrGroupId, @RequestParam Map<String, Object> params){
        PageUtils page = attrAttrgroupRelationService.queryNoAttrRelation(params,attrGroupId);
        return R.ok().put("page", page);
    }


    //获取属性分组关联的属性
    @RequestMapping("/{attrGroupId}/attr/relation")
    public R attrList(@PathVariable("attrGroupId") Long attrGroupId ){
        List<AttrEntity> list= attrAttrgroupRelationService.listAttrToGroup(attrGroupId);
        return R.ok().put("data",list);
    }


    /**
     * 列表
     */
    @RequestMapping("/list/{catelogId}")
    public R list(@RequestParam Map<String, Object> params, @PathVariable("catelogId") long catelogId){
        PageUtils page = attrGroupService.queryPage1(params,catelogId);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);

        Long[] catelogPath = categoryService.findCatelogPath(attrGroupId);
        attrGroup.setCatelogPath(catelogPath);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
